package com.dao;


import com.DBConfig.DBUtil;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
import com.pojo.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static Connection conn;
    static {
        conn = DBUtil.getConn();
    }
    public static List<User> getUserList() {
        Statement stmet = null;
        ResultSet result = null;
        List<User> users = new ArrayList<>();
        try {
            stmet = conn.createStatement();
            String sql = "select id,name,password,isAdmin from user";
            result = stmet.executeQuery(sql);
            while (result.next()) {
                User user = new User();
                user.setId(result.getString("id"));
                user.setName(result.getString("name"));
                user.setPassword(result.getString("password"));
                user.setAdmin(result.getBoolean("isAdmin"));
                users.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (result != null) result.close();
                if (stmet != null) stmet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return users;
    }
    public static boolean addUser(Object users[]){
        String sql="insert into user(id,name,password,isAdmin) values (?,?,?,?)";
        PreparedStatement preps=null;
        boolean execute=false;
        try {
            preps = conn.prepareStatement(sql);
            for (int i = 0; i < users.length; i++) {
                preps.setObject(i+1,users[i]);
            }
            execute = preps.execute();
            return true;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preps!=null)preps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static User getUserById(String id){
        String sql="select id,name,password,isAdmin from user where id=?";
        PreparedStatement ptst = null;
        ResultSet result = null;
        User user = null;
        try {
            ptst = conn.prepareStatement(sql);
            ptst.setString(1,id);
            result = ptst.executeQuery();
            while (result.next()) {
                user = new User();
                user.setId(result.getString("id"));
                user.setName(result.getString("name"));
                user.setPassword(result.getString("password"));
                user.setAdmin(result.getBoolean("isAdmin"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ptst!=null)ptst.close();
                if (result!=null) result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public static User getUserByUsernameAndPassword(String username, String password) {
        String sql="select id,name,password,isAdmin from user where name=? AND password=? ";
        PreparedStatement ptst = null;
        ResultSet result = null;
        User user = null;
        try {
            ptst = conn.prepareStatement(sql);
            ptst.setString(1,username);
            ptst.setString(2,password);
            result = ptst.executeQuery();
            while (result.next()) {
                user = new User();
                user.setId(result.getString("id"));
                user.setName(result.getString("name"));
                user.setPassword(result.getString("password"));
                user.setAdmin(result.getBoolean("isAdmin"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ptst!=null)ptst.close();
                if (result!=null) result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }
}
